package com.msl.debezium.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;

public class ReportUtil {
    //默认的生成文件的位置
    public static void createFile(String content){
        try {
            String basePath = System.getProperty("user.dir");
            String path = basePath + "\\log";

            String nowStr = DateUtil.dateToStr(new Date());
            String title = "cdcValidationReport" + nowStr;
            //防止文件创建或读取失败，用catch捕捉错误并打印，也可以throw
            File mkdirsName = new File(path);
            if(!mkdirsName.exists()){
                mkdirsName.mkdirs();
            }
            File writename = new File(path+"\\"+title+".txt");
            //文件不存在则新建
            if(!writename.exists()){
                writename.createNewFile();
            }else{
                // 根据操作系统添加换行符
                String osName = System.getProperties().getProperty("os.name");
                if(osName.equals("Linux")){
                    content = "\r" + content;
                }else{
                    content = "\r\n" + content;
                }
            }
            // 如果在原有基础之上写入则append属性为true，默认为false
            BufferedWriter out = new BufferedWriter(new FileWriter(writename, true));
            out.write(content);
            // 把缓存区内容压入文件
            out.flush();
            // 关闭流
            out.close();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
